package archer.framework.security.token.impl;

import archer.framework.security.token.Token;
import archer.framework.security.token.TokenDao;
import archer.framework.utils.ValidateUtils;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * @author christ
 * @date 2016/5/9
 */
public class MemoryStoreTokenDao implements TokenDao {

    private ConcurrentMap<Object, Token> tokens = new ConcurrentHashMap<>();

    @Override
    public void create(Token token) {

        if (token == null || ValidateUtils.isEmpty(token.getId())) {
            throw new NullPointerException("token can not be null");
        }

        tokens.putIfAbsent(token.getId(), token);
    }

    @Override
    public void update(Token token) {

        if (token == null || ValidateUtils.isEmpty(token.getId())) {
            throw new NullPointerException("token can not be null");
        }

        tokens.put(token.getId(), token);
    }

    @Override
    public Token find(String id) {

        return tokens.get(id);
    }

    @Override
    public void delete(String id) {

        tokens.remove(id);
    }
}
